package com.ruicar.afs.cloud.finance.mq.receiver.manage.processor;

import com.ruicar.afs.cloud.bizcommon.voucher.service.VoucherFlowService;
import com.ruicar.afs.cloud.common.modules.contract.enums.*;
import com.ruicar.afs.cloud.common.modules.dto.mq.voucher.VoucherFlowInfoDto;
import com.ruicar.afs.cloud.common.mq.finance.corporate.dto.GeneraSuccessDto;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.finance.common.entity.FinanceOperateDetail;
import com.ruicar.afs.cloud.finance.common.entity.FinanceReceiptOperate;
import com.ruicar.afs.cloud.finance.common.entity.FinanceTransLine;
import com.ruicar.afs.cloud.finance.common.service.FinanceOperateDetailService;
import com.ruicar.afs.cloud.finance.common.service.FinanceReceiptOperateService;
import com.ruicar.afs.cloud.finance.common.service.FinanceTransLineService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;

/**
 * <p>Description: </p>
 *
 * @author ZC.GUO
 * @version 1.0
 * @date create on 2020-07-20 10:20
 */
@Slf4j
@Component
@AllArgsConstructor
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
public class GeneraSuccessProcessor implements AfsMqBizProcessor<GeneraSuccessDto> {

    private final FinanceTransLineService financeTransLineService;
    private final FinanceReceiptOperateService financeReceiptOperateService;
    private final FinanceOperateDetailService financeOperateDetailService;
    private final VoucherFlowService voucherFlowService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean processMessage(GeneraSuccessDto entity) throws Exception {
        return this.transData(entity);
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_CONTRACT_MANAGE_FINANCE_CONTRACT_GENERA_SUCCESS;
    }

    private boolean transData(GeneraSuccessDto generaSuccessDto) {
        /**  记录业务操作记录 by ZC.GUO  **/
        FinanceTransLine financeTransLine = this.handleTransLine(generaSuccessDto);

        /**  记录来款操作记录 by ZC.GUO  **/
        FinanceReceiptOperate financeReceiptOperate = this.handleReceiptOperate(generaSuccessDto, financeTransLine);

        /**  记录来款操作明细 by ZC.GUO  **/
        this.handleOperateDetail(generaSuccessDto, financeTransLine, financeReceiptOperate);
        VoucherFlowInfoDto voucherFlowInfoDto = new VoucherFlowInfoDto();
        voucherFlowInfoDto.setTransNo(financeReceiptOperate.getId().toString());
        voucherFlowInfoDto.setDealerName(generaSuccessDto.getDealerName());
        voucherFlowInfoDto.setBuriedPointNo(generaSuccessDto.getBuriedPointNo());
        voucherFlowInfoDto.setFinancialProducts(generaSuccessDto.getFinancialProducts());
        voucherFlowInfoDto.setContractNo(generaSuccessDto.getContractNo());
        voucherFlowInfoDto.setKeepAccountDate(new Date());
        voucherFlowService.sendFlow(voucherFlowInfoDto);
        return true;
    }

    private FinanceOperateDetail handleOperateDetail(GeneraSuccessDto generaSuccessDto, FinanceTransLine financeTransLine,FinanceReceiptOperate financeReceiptOperate){
        FinanceOperateDetail financeOperateDetail = new FinanceOperateDetail();
        financeOperateDetail.setAccountId(null);
        financeOperateDetail.setOperateId(financeReceiptOperate.getId());
        financeOperateDetail.setFeeType(FeeTypeEnum.unusedFund);
        financeOperateDetail.setFeeSourceTable(generaSuccessDto.getFeeSourceTable());
        financeOperateDetail.setFeeId(generaSuccessDto.getFundId());
        financeOperateDetail.setReceiptDetailId(generaSuccessDto.getFundId());
        financeOperateDetail.setAmount(generaSuccessDto.getAmount());
        financeOperateDetail.setPayAccount(generaSuccessDto.getSourceAccount());
        financeOperateDetail.setReceiptAccount(generaSuccessDto.getCreditAccount());
        financeOperateDetailService.save(financeOperateDetail);
        return financeOperateDetail;
    }

    /**
     * 新增来款操作记录
     * @author ZC.GUO
     * @param
     * @return
     */
    private FinanceReceiptOperate handleReceiptOperate(GeneraSuccessDto generaSuccessDto, FinanceTransLine financeTransLine){
        FinanceReceiptOperate financeReceiptOperate = new FinanceReceiptOperate();
        financeReceiptOperate.setTransId(financeTransLine.getId());
        financeReceiptOperate.setFundsType(ReceiptSourceTypeEnum.transferAccounts);
        financeReceiptOperate.setFundsId(generaSuccessDto.getFundId());
        financeReceiptOperate.setTime(Calendar.getInstance().getTime());
        financeReceiptOperate.setType(ReceiptOperateTypeEnums.arrive);
        financeReceiptOperate.setAmount(generaSuccessDto.getAmount());
        financeReceiptOperateService.save(financeReceiptOperate);
        return  financeReceiptOperate;
    }

    /**
     * 新增业务操作记录
     * @author ZC.GUO
     * @param
     * @return
     */
    private FinanceTransLine handleTransLine(GeneraSuccessDto generaSuccessDto){
        FinanceTransLine financeTransLine = new FinanceTransLine();
        financeTransLine.setCustNo(null);
        financeTransLine.setOperateType(TransOpreateTypeEnums.bankarrive);
        financeTransLine.setOperator(generaSuccessDto.getOperator());
        financeTransLine.setOperateResult(ReturnCodeEnum.success);
        financeTransLine.setOperateTime(Calendar.getInstance().getTime());
        financeTransLineService.save(financeTransLine);
        return  financeTransLine;
    }
}
